package com.zch.zhongchou.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.zch.zhongchou.dao.ProjectsMapper;
import com.zch.zhongchou.pojo.Projects;
import com.zch.zhongchou.pojo.User;
import com.zch.zhongchou.service.ProjectService;
import com.zch.zhongchou.util.PageSize;
@Service("ps")
public class ProjectsServiceImpl implements ProjectService {

	@Autowired
	private ProjectsMapper dao;
	@Override
	public int add(Projects project) {
		// TODO Auto-generated method stub
		return dao.insert(project);
	}

	@Override
	public int delete(int id) {
		// TODO Auto-generated method stub
		return dao.deleteByPrimaryKey(id);
	}

	@Override
	public int update(Projects project) {
		// TODO Auto-generated method stub
		return dao.updateByPrimaryKey(project);
	}

	@Override
	public Projects getProjectById(int id) {
		// TODO Auto-generated method stub
		return dao.selectByPrimaryKey(id);
	}

	@Override
	public List<Projects> getAllProjectsByPage(String proName, String userName, int proStatus, int proType,
			int pageIndex) {
		Map<String, Object> map = new HashMap<>();
		map.put("proName", proName);
		map.put("userName", userName);
		map.put("proStatus", proStatus);
		map.put("proType", proType);
		int firstRow = (pageIndex - 1)*PageSize.PAGE_SIZE;
		int lastRow = PageSize.PAGE_SIZE;
		map.put("firstRow", firstRow);
		map.put("lastRow", lastRow);
		List<Projects> list = dao.getAllProjectsByPage(map);
		return list;
	}

	@Override
	public int getTotalCounts(String proName, String userName, int proStatus, int proType) {
		Map<String, Object> map = new HashMap<>();
		map.put("proName", proName);
		map.put("userName", userName);
		map.put("proStatus", proStatus);
		map.put("proType", proType);
		int counts = dao.getTotalCounts(map);
		return counts;
	}

	@Override
	public int getTotalPages(String proName, String userName, int proStatus, int proType) {
		int counts = getTotalCounts(proName, userName, proStatus, proType);
		int totalPages = counts % PageSize.PAGE_SIZE == 0 ? 
				counts/PageSize.PAGE_SIZE :counts/PageSize.PAGE_SIZE+1;
		return totalPages;
	}

}
